package com.dyz.leetcode._13_binaryTree;

import com.dyz.leetcode.TreeNode;

import java.util.*;

public class _107_levelOrderBottom {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root==null){return res;}
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            //每轮循环遍历处理一层节点
            int size = queue.size();
            List<Integer> listLevel = new ArrayList<>();
            for(int i=0; i<size; i++){
                TreeNode curr = queue.poll();
                listLevel.add(curr.val);
                if(curr.left!=null){queue.add(curr.left);}
                if(curr.right!=null){queue.add(curr.right);}
            }
            res.add(listLevel);
        }
        Collections.reverse(res);
        return res;

    }
}
